package com.borun.dictionary;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.borun.bean.Word;
import com.borun.dao.WordDao;

public class ReciteWord extends JFrame implements ActionListener {
	private WordDao dao = new WordDao();
	private static final long serialVersionUID = 1L;
	private Map<String, String> wordMap = new HashMap<>();
	private String[] keys = new String[10];
	private JPanel panelTop, panelCenter, panelBottom;
	private JLabel lblScore, lblOrder, lblQuestion;
	private JTextField txtAnswer;
	private JButton btnPrev, btnTip, btnSkip, btnNext;

	private int score = 0, index = 1;

	public ReciteWord(int baseid) {
		super("我爱记单词");
		this.setLayout(new BorderLayout());

		try {

			List<Word> words = dao.getWords(baseid);
			Collections.shuffle(words); // 打乱次序
			words = words.subList(0, 10); // 取出10个

			for (int i = 0; i < words.size(); i++) {
				wordMap.put(words.get(i).getEnglish(), words.get(i)
						.getChinese());
				keys[i] = words.get(i).getEnglish();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		panelTop = new JPanel();
		panelTop.setLayout(new FlowLayout(FlowLayout.RIGHT, 30, 5));
		panelTop.setSize(400, 30);

		lblScore = new JLabel("当前得分：" + score);
		lblScore.setForeground(Color.RED);
		panelTop.add(lblScore);

		panelCenter = new JPanel();
		panelCenter.setLayout(new FlowLayout(FlowLayout.CENTER, 200, 15));
		panelCenter.setSize(400, 200);

		lblOrder = new JLabel("10 / " + index);
		lblOrder.setFont(new Font("", Font.BOLD, 16));

		lblQuestion = new JLabel(wordMap.get(keys[0]));
		lblQuestion.setForeground(Color.BLUE);
		lblQuestion.setFont(new Font("", Font.BOLD, 40));

		txtAnswer = new JTextField(16);
		txtAnswer.setFont(new Font("", Font.PLAIN, 18));

		panelCenter.add(lblOrder);
		panelCenter.add(lblQuestion);
		panelCenter.add(txtAnswer);

		panelBottom = new JPanel();
		panelBottom.setLayout(new FlowLayout(FlowLayout.LEFT, 25, 15));
		panelBottom.setSize(400, 70);

		btnPrev = new JButton("上一个");
		btnPrev.addActionListener(this);
		btnPrev.setEnabled(false);

		btnTip = new JButton("提示");
		btnTip.addActionListener(this);

		btnSkip = new JButton("跳过");
		btnSkip.addActionListener(this);

		btnNext = new JButton("下一个");
		btnNext.addActionListener(this);

		panelBottom.add(btnPrev);
		panelBottom.add(btnTip);
		panelBottom.add(btnSkip);
		panelBottom.add(btnNext);

		this.add(panelTop, BorderLayout.NORTH);
		this.add(panelCenter, BorderLayout.CENTER);
		this.add(panelBottom, BorderLayout.SOUTH);

		this.setSize(400, 300);
		this.setLocationRelativeTo(null); // 设置窗口相对于指定组件的位置
		this.setResizable(false); // 禁止改变窗体大小
		// this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
	}

//	public static void main(String[] args) {
//		new ReciteWord(1);
//	}

	boolean isOver = false;

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnPrev) {
			if (isOver) {
				index--;
				if (index <= 1) {
					btnPrev.setEnabled(false);
					btnNext.setEnabled(true);
				}
				lblOrder.setText("10 / " + index);
				lblQuestion.setText(wordMap.get(keys[index - 1]));
				txtAnswer.setText(keys[index - 1]);
				txtAnswer.setEnabled(false);
				return;
			}
		} else if (e.getSource() == btnTip) {
			String tipString = keys[index - 1];
			switch (tipString.length()) {
			case 1:
				tipString = tipString.trim();
				break;
			case 2:
				tipString = tipString.trim();
				break;
			case 3:
			case 4:
				tipString = tipString.replace(tipString.charAt(1), '_');
				break;
			case 5:
			case 6:
				tipString = tipString.replace(tipString.charAt(2), '_')
						.replace(tipString.charAt(3), '_');
				break;
			default:
				tipString = tipString.replace(tipString.charAt(2), '_')
						.replace(tipString.charAt(3), '_')
						.replace(tipString.charAt(4), '_');
				break;
			}
			JOptionPane.showMessageDialog(null, "温馨提示 ：    " + tipString);
		} else if (e.getSource() == btnSkip) {
			index++;
			if (index == 10) {
				btnSkip.setEnabled(false);
			}
			lblOrder.setText("10 / " + index);
			lblQuestion.setText(wordMap.get(keys[index - 1]));
		} else if (e.getSource() == btnNext) {
			if (isOver) {
				index++;
				if (index == 10) {
					btnNext.setEnabled(false);
					btnSkip.setEnabled(false);
					btnTip.setEnabled(false);
					btnPrev.setEnabled(true);
				}
				lblOrder.setText("10 / " + index);
				lblQuestion.setText(wordMap.get(keys[index - 1]));
				txtAnswer.setText(keys[index - 1]);
				return;
			}

			if (index == 11) {
				switch (score) {
				case 100:
					JOptionPane.showMessageDialog(null, "牛B了呀，学霸从此诞生！");
					break;
				case 90:
					JOptionPane.showMessageDialog(null, "很不错嘛，加油！下一个学霸就是你了");
					break;
				case 80:
					JOptionPane.showMessageDialog(null, "很不错了，加油，争取下次拿优秀！");
					break;
				case 70:
					JOptionPane.showMessageDialog(null,
							"再加把劲！别总是在原地踏步，争取下次拿优秀！");
					break;
				case 60:
					JOptionPane.showMessageDialog(null, "菜鸡，刚刚在及格边缘，你很危险的啦！");
					break;
				default:
					JOptionPane.showMessageDialog(null,
							"辣鸡，这么简单都不及格，你要好好反思一下咯！");
					break;
				}
				isOver = true;
				btnPrev.setEnabled(true);
				btnNext.setEnabled(false);
				btnSkip.setEnabled(false);
				btnTip.setEnabled(false);
				txtAnswer.setEnabled(false);
				index = 10;
				return;
			}

			String answer = txtAnswer.getText();
			if ("".equals(answer.trim())) {
				JOptionPane.showMessageDialog(null, "必须先填写答案或者点击【跳过】");
				return;
			}

			if (answer.equals(keys[index - 1])) {
				if (index < 10) {
					txtAnswer.setText(null);
				}
				score += 10;
				lblScore.setText("当前得分：" + score);

				index++;
				if (index < 11) {
					lblOrder.setText("10 / " + index);
					lblQuestion.setText(wordMap.get(keys[index - 1]));
				} else {
					return;
				}

			} else {
				int result = JOptionPane.showConfirmDialog(null,
						"该题回答错误，确认要下一题？", "提示", JOptionPane.OK_CANCEL_OPTION);
				if (result == JOptionPane.OK_OPTION) {
					index++;
					if (index < 11) {
						lblOrder.setText("10 / " + index);
						lblQuestion.setText(wordMap.get(keys[index - 1]));
					} else {
						return;
					}
				}
			}
		}
	}
}
